/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.ms1_intensitiesretrieval;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Jonas
 */
public class ExecuteProgram {

    // Initiate logger and the projects map to hold the experiments
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(ExecuteProgram.class);
    private Map<Integer, String[]> projects;

    public ExecuteProgram() {

        projects = new HashMap<Integer, String[]>();

        String[] runs_1 = {"8741", "8741", "8742", "8743", "8744", "8745", "8746", "8747", "8748", "8749", "8750", "8751", "8752", "8753", "8754", "8755", "8756", "8757", "8758", "8759"};
        projects.put(1918, runs_1);
        String[] runs_2 = {"9017", "9018", "9019", "9020", "9021", "9022", "9023", "9024", "9025", "9026", "9027", "9028", "9029", "9030", "9031", "9032", "9033", "9034", "9035", "9036"};
        projects.put(1941, runs_2);
        String[] runs_3 = {"9040", "9041", "9042", "9043", "9044", "9045", "9046", "9047", "9048", "9049", "9050", "9051", "9052", "9053", "9054", "9055", "9056", "9057", "9058", "9059"};
        projects.put(1942, runs_3);
    }

    // Main class that starts the program
    public static void main(String[] args) {
        ExecuteProgram executeProgram = new ExecuteProgram();
        executeProgram.doMain();
    }

    public void doMain() {
        DataProvider dataProvider = new DataProvider();
        FeatureXMLReader featureXMLReader = new FeatureXMLReader();
        IdentificationWindowUpdater identificationWindowUpdater = new IdentificationWindowUpdater();
        MzmlIterator mzmlIterator = new MzmlIterator();
        OutputWriter outputWriter = new OutputWriter();

        for (int projectID : projects.keySet()) {
            String[] runs = projects.get(projectID);
            for (int i = 0; i < runs.length; i++) {
                System.out.println("");
                LOGGER.info("Processing projectID " + projectID + " Run " + (i + 1));

                // Execute the ms_lims query
                List<MSLimsHits> identificationsList = dataProvider.querySpectrumVariables(projectID, runs[i]);

                // Search the right FeatureXMLFile
                File parentDirectory = new File("C:\\Test\\Giulia_pyrococcus\\AfterFeatureFinding\\" + projectID);
                File featureXMLFile = findFileByRunNumber(parentDirectory, runs[i]);
                // Parse the FeatureXMLFile into a featureList
                List<FeatureVariables> featuresList = featureXMLReader.parseFeatureXML(featureXMLFile);

                // Match the correct windows to the identifications
                List<MSLimsHits> windowedIdentifications = identificationWindowUpdater.compareFeaturesToIdentifications(identificationsList, featuresList);

                // Look for the right mzmlFile
                parentDirectory = new File("C:\\Test\\Giulia_pyrococcus\\AfterPeakPicking\\" + projectID);
                File mzmlFile = findFileByRunNumber(parentDirectory, runs[i]);
                // Loop through the mzmlFile
                mzmlIterator.iterateAllSpectra(windowedIdentifications, mzmlFile);


                //Write the updated MS2Identifications to file
                File completedFile = new File("C:\\Test\\Giulia_pyrococcus\\WithAddedMS1Intensities\\" + projectID + "\\" + runs[i] + ".csv");
                outputWriter.writeSpectrumVariablesToFile(windowedIdentifications, completedFile, projectID);
            }
        }
    }

    // Check for the right mzmlFile by runNumber in the parentDirectory
    public File findFileByRunNumber(File parentDirectory, String runNumber) {
        File foundFile = null;

        if (parentDirectory.isDirectory()) {

            File[] files = parentDirectory.listFiles();
            for (File file : files) {
                if (file.getName().contains(runNumber)) {
                    foundFile = file;
                    break;
                }
            }
        }
        return foundFile;
    }
}
